package com.wu.mvp.app.base.list;

import android.content.Context;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.view.ViewGroup;

import com.wu.mvp.app.base.BasePresenter;
import com.wu.mvp.config.Constant;
import com.wu.mvp.widget.recyclerview.adapter.BaseViewHolder;
import com.wu.mvp.widget.recyclerview.adapter.RecyclerArrayAdapter;

import java.util.List;

public class BaseListPresenter<V extends BaseListActivity, M> extends BasePresenter<V>
        implements RecyclerArrayAdapter.OnItemClickListener,RecyclerArrayAdapter.OnItemLongClickListener,
        RecyclerArrayAdapter.onLoadMoreListener, RecyclerArrayAdapter.onRefreshListener {

    private DataAdapter adapter;
    private Handler refreshHandler;
    private Handler loadHandler;

    @Override
    protected void onCreateView(@NonNull V view) {
        super.onCreateView(view);
        refreshHandler = new Handler();
        loadHandler = new Handler();
        getAdapter().setOnItemClickListener(this);
        adapter.setOnItemLongClickListener(this);
        adapter.setLoadMoreListener(this);
        adapter.setRefreshListener(this);
    }

    /**
     * 获取recyclerView的adapter
     * @return
     */
    public DataAdapter getAdapter() {
        if (getView().mCtx == null) {
            throw new RuntimeException("you shouldn't use getView() at Presenter's onCreate(),onCreateView() may invoke more than once,you should put the config code into onCreateView()");
        }
        if (adapter == null) {
            adapter = new DataAdapter(getView().mCtx);
        }
        return adapter;
    }

    /**
     * 刷新超时取消刷新
     */
    public void overTimeRefresh() {
        if (refreshHandler == null || !getView().isRefresh) {
            return;
        }
        refreshHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                getView().stopRefresh(false);
            }
        }, Constant.LOAD_OVERTIME);
    }

    /**
     * 加载超时取消加载
     */
    public void overTimeLoadMore() {
        if (loadHandler == null || !getView().isLoadMore) {
            return;
        }
        loadHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                getView().stopLoadMore(false);
            }
        }, Constant.LOAD_OVERTIME);
    }

    /**
     * 刷新成功，当完成刷新操作后必须调用此函数，不然刷新框会等到超时后显示刷新失败
     */
    public void refreshSuccess() {
        if (refreshHandler != null) {
            refreshHandler.removeCallbacksAndMessages(null);
        }
        getView().stopRefresh(true);
    }

    /**
     * 加载成功，当完成加载操作后必须调用此函数，不然加载框会等到超时后显示加载失败
     */
    public void loadMoreSuccess() {
        if (loadHandler != null) {
            loadHandler.removeCallbacksAndMessages(null);
        }
        getView().stopLoadMore(true);
    }

    /**
     * 开放接口实现刷新操作
     */
    @Override
    public void onRefresh() {
        overTimeRefresh();
    }

    /**
     * 开放接口实现加载操作
     */
    @Override
    public void onLoadMore() {
        overTimeLoadMore();
    }

    /**
     * 开放接口实现点击item操作
     * @param position
     */
    @Override
    public void onItemClick(int position) {

    }

    /**
     * 开放接口实现长按item操作
     * @param position
     * @return
     */
    @Override
    public boolean onItemLongClick(int position) {
        return false;
    }


    public class DataAdapter extends RecyclerArrayAdapter<M> {

        public DataAdapter(Context context) {
            super(context);
        }

        public DataAdapter(Context context, List<M> objects) {
            super(context, objects);
        }

        @Override
        public int getViewType(int position) {
            return getView().getViewType(position);
        }

        @Override
        public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
            return getView().getViewHolder(parent, viewType);
        }
    }


}
